package com.hadoop.mr.reduceJoin;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import static org.apache.commons.beanutils.BeanUtils.copyProperties;

/**
 * @program: hadoop
 * @description:
 * @author: Qiang.Ye
 * @date: 2019-06-09 12:19
 **/
public class TableReducer extends Reducer<Text,TableBean,TableBean, NullWritable> {
    @Override
    protected void reduce(Text key,
                          Iterable<TableBean> values,
                          Context context) throws IOException, InterruptedException {
        List<TableBean> orderBeanList = new ArrayList<>();
        TableBean pdBean  = new TableBean();
        for (TableBean  bean : values){

            //0表示订单的处理
            if("0".equals(bean.getFlag())){
                TableBean orderBean = new TableBean();
                try {
                    copyProperties(orderBean,bean);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                orderBeanList.add(orderBean);
            }else {
                //1是产品表处理
                try {
                    copyProperties(pdBean,bean);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                orderBeanList.add(pdBean);
            }
        }
        for (TableBean b : orderBeanList){
            b.setPname(pdBean.getPname());
            context.write(b,NullWritable.get());
        }


    }
}
